Add ExpandEnumMethods to expand enum extension calls into ternary expressions#150
Add ExpandEnumMethods to expand enum extension calls into ternary expressions#150
Conversation
…enum method expansion Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
|
@copilot I see that methods interpretation is not straightforward at all during source generation, so I want to try something, using the ProjectableEnumMethodAttribute which tends to describe extension methods for what they return, it can be used without argument if the method returns the enum member attribute of type T, one argument (the attribute type) if it returns the attribute of this type from the enum member, or two args : attribute type and property name : if the method returns the attribute's property value. |
…ation Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Implemented in becae46. Added
The generator now requires this attribute on any method called with Added functional tests that verify the SQL generation for filtering, selecting, and ordering by expanded enum properties. |
|
@copilot I was wrong to want to in-line attribute values in the expression tree. All we need is to call the enum methods on the enum members, in the expression tree and let EF do the rest. |
…ning values Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Implemented in f42ef49. Removed Example generated expression: @this.Status == Status.Pending ? EnumExtensions.GetDisplayName(Status.Pending) : @this.Status == Status.Approved ? EnumExtensions.GetDisplayName(Status.Approved) : nullAdded a test for enum members on navigation properties as requested. |
|
@copilot Add new tests (unit et integration) that validate that other types are supported for enum expansion : boolean, integer, etc, and add new test showing that enum extension methods with parameters work well, fix if it does not work. |
…rs, update README Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Implemented in e7b388e. Added:
|
There was a problem hiding this comment.
Pull request overview
This pull request adds a new ExpandEnumMethods feature to the ProjectableAttribute that enables enum extension method calls to be expanded into ternary expressions, allowing EF Core to translate them into SQL CASE statements. This is particularly useful for Where() and OrderBy() clauses where enum extension methods (like getting display names from attributes) need to be translated to SQL.
Changes:
- Added
ExpandEnumMethodsboolean property toProjectableAttribute - Implemented enum method expansion logic in
ExpressionSyntaxRewriterthat generates ternary expressions for each enum value - Added comprehensive test coverage including generator tests and functional tests with verified SQL output
- Updated README with detailed documentation and examples
Reviewed changes
Copilot reviewed 41 out of 41 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| src/EntityFrameworkCore.Projectables.Abstractions/ProjectableAttribute.cs | Adds ExpandEnumMethods property with XML documentation |
| src/EntityFrameworkCore.Projectables.Generator/ProjectableInterpreter.cs | Extracts ExpandEnumMethods attribute value and passes to ExpressionSyntaxRewriter |
| src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.cs | Core implementation: expands enum method calls into ternary expressions, handles nullable enums, supports different return types |
| tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs | Generator tests covering display attributes, descriptions, nullable enums, navigation properties, boolean/integer returns, and parameterized methods |
| tests/EntityFrameworkCore.Projectables.Generator.Tests/*.verified.txt | Verified expected output for all generator tests |
| tests/EntityFrameworkCore.Projectables.FunctionalTests/ExpandEnumMethodsTests.cs | Functional tests verifying SQL generation for filtering, selecting, ordering, and various enum scenarios |
| tests/EntityFrameworkCore.Projectables.FunctionalTests/*.verified.txt | Verified SQL output for all functional tests across different .NET versions |
| README.md | Comprehensive documentation with examples, generated expressions, and SQL output |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.cs
Show resolved
Hide resolved
src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.cs
Show resolved
Hide resolved
src/EntityFrameworkCore.Projectables.Abstractions/ProjectableAttribute.cs
Outdated
Show resolved
Hide resolved
src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.cs
Outdated
Show resolved
Hide resolved
tests/EntityFrameworkCore.Projectables.FunctionalTests/ExpandEnumMethodsTests.cs
Show resolved
Hide resolved
src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.cs
Outdated
Show resolved
Hide resolved
src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.cs
Outdated
Show resolved
Hide resolved
# Conflicts: # src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.cs # src/EntityFrameworkCore.Projectables.Generator/ProjectableInterpreter.cs # tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 41 out of 41 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Adds a new
ExpandEnumMethodsproperty to theProjectableAttributethat expands enum extension method calls into a chain of ternary expressions for each enum value. This enables EF Core to translate these expressions to SQL CASE statements, making enum methods usable inWhere()andOrderBy()clauses.How it works
When
ExpandEnumMethods = trueis set on a projectable property, enum method calls are expanded into ternary expressions that call the method on each enum member. EF Core then evaluates these method calls at runtime.Supported return types
nullas fallbackdefault(bool)(false) as fallbackdefault(int)(0) as fallbackdefault(T)as fallbackFeatures
Example
SQL Generated
EF Core translates the expanded expressions to CASE statements:
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.